<template>
  <div class="select-view-box">
    <el-select
      v-model="selectValue"
      :placeholder="placeholder"
      @change="clientType"
      :multiple="multiple?true:false"
      :collapse-tags="multiple?true:false"
      clearable 
    >
      <el-option
        v-for="item in optionList"
        :key="item[valueName]"
        :label="item[labelName]"
        :value="item[valueName]"
      ></el-option>
    </el-select>
  </div>
</template>

<script>
export default {
  name: "SelectView",
  props: {
    label: {
      type: String,
      default: "",
    },
    optionList: {
      type: Array,
      default: () => {
        return [];
      },
    },
    placeholder:{
      type:String,
      default:""
    },
    value: {
      type: Number | String | Array,
      default: "",
    },
    type: {
      type: String,
      default: "",
    },
    labelName: {
      type: String,
      default: "label",
    },
    valueName: {
      type: String,
      default: "value",
    },
    multiple: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      selectValue: this.value,
    };
  },
  watch: {
    value: function (value) {
      if(this.multiple){
        this.selectValue = value.split(',').map(number)
      }else{
        this.selectValue = value;
      }
    },
  },
  methods: {
    clientType() {
      this.$emit("value", { value: this.selectValue, type: this.type });
    },
  },
};
</script>

<style lang="scss">
  .el-select__tags-text {
    display: inline-block;
    max-width: 20px;
    overflow: hidden;
    text-overflow:ellipsis;
    white-space: nowrap;
  }
  .el-select .el-tag__close.el-icon-close {
    top: -7px;
    right: -4px;
  }
</style>
<style lang="scss" scoped>
.select-view-box {
  margin-right: 10px;
  width: 140px;
}
</style>